/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:08
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.repository;

import java.util.List;

import com.joysuch.wwyt.edu.bean.EduSourceDto;
import com.joysuch.wwyt.edu.entity.EduTestPaper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

/**
 * EduTestPaperDao
 *
 */
public interface EduTestPaperDao extends JpaRepository<EduTestPaper, Long>, QuerydslPredicateExecutor<EduTestPaper> {
    public Page<EduTestPaper> findAll(Specification<EduTestPaper> spec, Pageable pageable);

    public EduTestPaper save(EduTestPaper bean);

    public void delete(EduTestPaper bean);

    @Query("select count(m) from EduTestPaper m where code = ?1")
    long countOfCode(String code);

    @Query("select count(m) from EduTestPaper m where code = ?1 and id != ?2")
    long countOfCodeExcludeId(String code, Long id);

    @Query("select count(m) from EduTestPaperQuestion m where testPaperId = ?1 ")
    public Integer countOfQuestion(Long id);

    @Query("select count(m) from EduExamTestPaper m where testPaperId = ?1 ")
    public Integer countOfExam(Long id);

    @Query("select p.passScore from EduExamTestPaper m, EduTestPaper p where m.testPaperId = p.id and m.examId=?1 ")
    public List<Double> queryPassScoreOfExam(Long examId);

    @Query(value = "SELECT\tpaper.* FROM\t`wwyt_edu_test_paper` paper\n" +
            "\tLEFT JOIN wwyt_edu_exam_test_paper exam ON paper.id = exam.TEST_PAPER_ID\n" +
            "\tWHERE EXAM_ID=?1 and exam.DELETE_FLAG= '0' order by exam.id desc limit 1",nativeQuery = true)
    EduTestPaper findByExamId(Long examId);
}
